﻿#region Copyright(C) 2014 NCS Pte. Ltd. All rights reserved.

// ==================================================================================================
// Copyright(C) 2006 NCS Pte. Ltd. All rights reserved.
//
// SYSTEM NAME	:   iConnect .Net Application Framework
// COMPONENT ID :   NCS.IConnect.Core.Mvc
// COMPONENT DESC:  Core components integrated with ASP.NET MVC
//
// CREATED DATE/BY:  19 May 2014 / Jiang Jin Nan
//
// REVISION HISTORY:	
// DATE/BY			SR#/CS/PM#/OTHERS		DESCRIPTION OF CHANGE
// ==================================================================================================

#endregion

using NCS.IConnect.Core.IoC;
using NCS.IConnect.Core.Utilities;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace NCS.IConnect.Core.Mvc
{
    /// <summary>
    /// A custom DependencyResolver which uses ServiceLocator to acticvate service.
    /// </summary>
    public class ServiceLocatableDependencyResolver : IDependencyResolver
    {
        #region Properties
        /// <summary>
        /// Gets the service locator.
        /// </summary>
        public IServiceLocator ServiceLocator { get; private set; }
        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ServiceLocatableDependencyResolver"/> class.
        /// </summary>
        /// <param name="serviceLocatorName">Name of the service locator.</param>
        public ServiceLocatableDependencyResolver(string serviceLocatorName = "")
        {
            if (string.IsNullOrEmpty(serviceLocatorName))
            {
                this.ServiceLocator = ServiceLocatorFactory.GetServiceLocator();
            }
            else
            {
                this.ServiceLocator = ServiceLocatorFactory.GetServiceLocator(serviceLocatorName);
            }
        }
        #endregion

        /// <summary>
        /// Gets the service.
        /// </summary>
        /// <param name="serviceType">Type of the service.</param>
        /// <returns>The activated service instance.</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
        public object GetService(Type serviceType)
        {
            Guard.ArgumentNotNull(serviceType, "serviceType");
            try
            {
                return this.ServiceLocator.GetService(serviceType);
            }
            catch (ResolutionException)
            {
                return null;
            }
        }

        /// <summary>
        /// Gets the services.
        /// </summary>
        /// <param name="serviceType">Type of the service.</param>
        /// <returns>The activated service instances.</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
        public IEnumerable<object> GetServices(Type serviceType)
        {
            Guard.ArgumentNotNull(serviceType, "serviceType");
            try
            {
                return this.ServiceLocator.GetAllServices(serviceType);
            }
            catch (ResolutionException)
            {
                return null;
            }
        }
    }
}
